const { staticUrl } = require("../../../services/config");

const app = getApp();
const { AskPage } = app._require("common/index");
const formMixin = app._require("mixins/form.mixin.js").default;
AskPage({
    name: "projects-manager-details",
    mixins: [formMixin],
    data: {
        scheduleData: [],
        listItems: ["当前阶段", "阶段进度"],
    },
    onShow: function (options) {
        //  console.log(this.data.type);
        if (this.data.type == "reload") {
            this.projectListByPage(this.data.projectId), this.projectDetails(this.data.projectId);
        }
    },
    onLoad: function (options) {
        let id = options.id || "1";
        this.setData({
            projectId: id,
        });
        // console.log(id);
        this.projectDetails(id), this.projectListByPage(id);
    },
    async projectListByPage(id) {
        const response = await this.$_server("project/listByProject", {
            params: {
                projectId: "?projectId=" + id,
            },
        });

        let { data } = response;
        if (data.code != 200) {
            wx.showToast({
                title: data.message,
                icon: "none",
            });
            //  return
        }
        let processList = data.data || [];
        processList.forEach(element => {
            let resImg = element.resImg;
            if (resImg) {
                resImg = JSON.parse(resImg);
                element.url = staticUrl + "/" + resImg[0];
            }

            // let createTime = element.updateTime??"";
            // let deadTime = element.completeTime??"";
            // if(!deadTime){deadTime=createTime}

            // // //计算时间差
            // var start_date = new Date().getTime();
            // var end_date = new Date(deadTime.replace(/-/g, "/"));
            // var days = end_date.getTime() - start_date;
            // var day = parseInt(days / (1000 * 60 * 60 * 24));
            // var hour = parseInt(days / (1000 * 60 * 60 ));

            // * 1 延迟
            //  * 2 正常
            //  * 3 提前
            // if(day==-0){day=-1}
            let state = element.state;
            if (state == 2) {
                element.type = "正常";
                element.type_color = "#4287FC";
            } else if (state == 3) {
                element.type = "提前";
                element.type_color = "#56C535";
            } else {
                //延期
                element.type = "延迟";
                element.type_color = "#DD1818";
            }
        });

        // if(processList.length==0){
        //    processList = [{"process":22,"content":"123123","updateTime":"2012-12-12 12:22:22"},
        //                   {"process":1,"content":"qweqweqew","updateTime":"2033-1-12 21:11:33"}]
        // }
        //  console.log(res.processList);
        let total = 0;
        try {
            total = data.data.total;
        } catch (e) {}
        this.setData({
            processList: processList,
            isFinish: processList.length >= total, //全部加载完毕
            type: "",
        });
        //  console.log(processList)
    },
    async projectDetails(id) {
        const response = await this.$_server("project/pdetail", {
            params: {
                id: "?id=" + id,
            },
        });
        let { data } = response;

        if (data.code != 200) {
            wx.showToast({
                title: data.message,
                icon: "none",
            });
            return;
        }

        let res = data.data;
        //  console.log(res);

        //当前阶段
        let stageName = res.stageName || "";
        //阶段进度
        let stageProcess = res.stageProcess || "0";
        if (stageProcess) {
            stageProcess = stageProcess + "%";
        }

        //  console.log(time)
        let list_items = [stageName, stageProcess];

        let process = res.process || "0";
        res.process = process;
        //  console.log(res.stage[res.stage.length-1].endDate)

        //数据包装
        let investor = res.investor || "";
        let totalInvestment = res.totalInvestment || "0";
        totalInvestment = totalInvestment / 10000 + "万元";

        let projectContent = res.projectContent || "";
        let leadName = res.leadName || "";
        let responsible = res.responsible || "";
        let address = res.address || "";
        let time = "";
        try {
            time = res.endDate || "";
        } catch (e) {}
        let show_items = [investor, totalInvestment, projectContent, address, leadName, responsible, time, stageName];

        this.setData({
            res: res,
            list_items: list_items,
            name: res.name,
            show_items: show_items,
            //  "scheduleData":res.processList
        });
    },
    uploadTaskProcess() {
        wx.navigateTo({
            url:
                "../../plugins/projects-manager/fill-process/index?id=" +
                this.data.projectId +
                "&name=" +
                this.data.name,
        });
        // console.log("上传进度");
    },
    clickShowBigImage(e) {
        // console.log(e);
        let index = e.currentTarget.dataset.index;
        let processList = this.data.processList;
        let item = processList[index];
        let resImg = item.resImg;
        let newResImg = [];
        if (resImg) {
            resImg = JSON.parse(resImg);
            resImg.forEach(element => {
                element = staticUrl + "/" + element;
                newResImg.push(element);
            });
        }
        if (resImg.length == 0) {
            return;
        }
        wx.previewImage({
            urls: newResImg, //需要预览的图片http链接列表，注意是数组
            current: "", // 当前显示图片的http链接，默认是第一个
            success: function (res) {},
            fail: function (res) {},
            complete: function (res) {},
        });
    },
    clickToDetails() {
        let json = JSON.stringify(this.data.show_items);
        console.log(json);
        wx.navigateTo({
            url:
                "./new-details/index?res=" + json + "&name=" + this.data.res.name + "&process=" + this.data.res.process+"&lngLat="+ this.data.res.lngLat,
        });
    },
    clickStageDetails(e) {
        let item = e.currentTarget.dataset.item;
        let queryData = {
            item: item,
            title: this.data.res.name,
        };
        try {
            wx.setStorageSync("queryData", JSON.stringify(queryData));
        } catch (e) {}
        wx.navigateTo({
            url: "./stage-details/index",
        });
    },
});
